Course Coordinates
- Taught at the University of Konstanz by Hansjörg Neth (h.neth@uni.kn, SPDS, office D507).
- Winter 2018/2019: Mondays, 13:30–15:00, C511.
- Links to current course syllabus | ZeUS | Ilias
Logo
Overview over generated plots:
Tiles
Showing the versions with borders:
Tile plots (random)
Tile plots (sorted)
Tile plots (with borders and numeric labels)
Random versions:
Sorted versions:
Poles
Showing the versions without borders:
Pole plots (random)
Pole plots (sorted)
Pole plots (with borders and numeric labels)
Random versions:
Sorted versions:
Settings
Text
# Define text labels:
lbl_psi <- expression(psi)
course_title <- paste0("Data science\nfor psychologists")
course_title_abb <- paste0("ds4psy")Data
# Dimensions:
n_rand <- sample(1:15, size = 1, replace = TRUE) # random integer in range
n_x <- n_rand # random number
# n_x <- 9 # a specific number
n_y <- n_x
N <- (n_x * n_y)
# Vectors:
v_sort <- 1:N # Tile: bottom = seeblau; top = black | Polar: outer = black, center = seeblau.
v_sort <- rev(1:N) # Tile: bottom = black; top = seeblau | Polar: outer = seeblau, center = black.
set.seed(123) # for reproducible randomness
# v_rand <- runif(n = N, 0, 1)
v_rand <- sample(v_sort, N) # random permutation of v_sort
# Table:
tb <- tibble(y = rep(1:n_x, each = n_y),
x = rep(1:n_y, times = n_x),
rand = v_rand,
sort = v_sort)
tb
#> # A tibble: 64 x 4
#> y x rand sort
#> <int> <int> <int> <int>
#> 1 1 1 46 64
#> 2 1 2 15 63
#> 3 1 3 39 62
#> 4 1 4 11 61
#> 5 1 5 8 60
#> 6 1 6 62 59
#> 7 1 7 34 58
#> 8 1 8 14 57
#> 9 2 1 7 56
#> 10 2 2 3 55
#> # … with 54 more rowsColors
# Re-sort color palette:
unikn.pal # from "ds4psy/R/custom_functions.R""
#> seeblau1 seeblau2 seeblau3 seeblau4 black seegrau4 seegrau3 seegrau2
#> 1 #CCEEF9 #A6E1F4 #59C7EB #00A9E0 #000000 #666666 #999999 #CCCCCC
#> seegrau1 white
#> 1 #E5E5E5 #FFFFFF
# (1) Sorted version:
unikn_sort <- unikn.pal[c(4, 4:1, 10:5)] # 11 colors (seeblau twice)
# unikn_sort
# (2) Sorted version with special cases (for small n_x):
if (n_x == 1) {
unikn_sort <- seeblau # 1 color (seeblau)
} else if (n_x == 2) {
unikn_sort <- unikn.pal[c(4, 2, 5, 9)] # 4 colors (black, but no white)
} else {
unikn_sort <- unikn.pal[c(4, 4:1, 10:5)] # 11 colors (seeblau twice)
}
unikn_sort
#> seeblau4 seeblau4.1 seeblau3 seeblau2 seeblau1 white seegrau1 seegrau2
#> 1 #00A9E0 #00A9E0 #59C7EB #A6E1F4 #CCEEF9 #FFFFFF #E5E5E5 #CCCCCC
#> seegrau3 seegrau4 black
#> 1 #999999 #666666 #000000Plot themes
ds_theme <- theme_bw() +
theme(panel.grid = element_blank(),
legend.position = "none",
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "white"),
panel.border = element_rect(color = grey(.25, 1)))
ds_theme <- theme_nothing()Tiles
Tile plots
- With thin borders:
# Parameters:
brd_col <- grey(0, 1)
brd_size <- .10
plot_size <- 3.0 # NORMAL: in cm (used in ggsave below): normal (small) size
# plot_size <- 10.0 # BIG: in cm (used in ggsave below): when "pix/big_"
pic_path <- "pix/"
# Tile plots: ------
# (1a) random version (WITH border lines):
tile_rand <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand), col = brd_col, size = brd_size) +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_rand
# Save current plot:
cur_name <- paste0("tile_rand_", n_x, "_brd.png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
# (1b) sorted version (WITH border lines):
tile_sort <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort), col = brd_col, size = brd_size) +
# coord_polar() +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "4", y = expression(psi)) +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_sort
# Save current plot:
cur_name <- paste0("tile_sort_", n_x, "_brd.png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)- Without thin borders:
# Tile plots: ------
# (2a) random version (withOUT border lines):
tile_rand <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand))+ #, col = brd_col, size = brd_size) +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_rand
# Save current plot:
cur_name <- paste0("tile_rand_", n_x, ".png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
# (2b) sorted version (withOUT border lines):
tile_sort <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort))+ #, col = brd_col, size = brd_size) +
# coord_polar() +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "4", y = expression(psi)) +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_sort
# Save current plot:
cur_name <- paste0("tile_sort_", n_x, ".png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)- With thin borders and a numeric label (top left):
# Parameters:
cur_lbl <- paste0(n_x)
x_lbl <- 1
y_lbl <- (n_y + 1) + n_y/15
# Tile plots: ------
# (3a) random version (WITH border lines AND label):
tile_rand_lbl <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand), col = brd_col, size = brd_size) +
geom_text(x = x_lbl, y = y_lbl, label = cur_lbl, size = 2) +
scale_y_continuous(limits = c(0, y_lbl)) +
# geom_text(x = 0, y = n_y, label = cur_lbl, size = 2) +
# scale_x_continuous(limits = c(0, n_x + 1)) +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_rand_lbl
# Save current plot:
cur_name <- paste0("tile_rand_", n_x, "_brd_lbl.png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
# (3b) sorted version (WITH border lines AND label):
tile_sort_lbl <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort), col = brd_col, size = brd_size) +
geom_text(x = x_lbl, y = y_lbl, label = cur_lbl, size = 2) +
scale_y_continuous(limits = c(0, y_lbl)) +
# geom_text(x = 0, y = n_y, label = cur_lbl, size = 2) +
# scale_x_continuous(limits = c(0, n_x + 1)) +
coord_fixed() +
# labs(title = "ds4psy") +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
tile_sort_lbl
# Save current plot:
cur_name <- paste0("tile_sort_", n_x, "_brd_lbl.png")
if (plot_size < 10) {
plot_name <- paste0(pic_path, cur_name)
} else {
plot_name <- paste0(pic_path, "big_", cur_name) # insert "big_" prefix
}
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)Polar plots
- With thin borders:
# Polar plots: ------
pole_rand <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand), col = brd_col, size = brd_size) +
# labs(title = "ds4psy") +
coord_polar() +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_rand
# Save current plot:
plot_name <- paste0("pix/pole_rand_", n_x, "_brd.png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
pole_sort <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort), col = brd_col, size = brd_size) +
labs(x = "4", y = expression(psi)) +
coord_polar() +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_sort
# Save current plot:
plot_name <- paste0("pix/pole_sort_", n_x, "_brd.png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)- Without thin borders:
# Polar plots: ------
pole_rand <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand)) + # , col = brd_col, size = brd_size) +
# labs(title = "ds4psy") +
coord_polar() +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_rand
# Save current plot:
plot_name <- paste0("pix/pole_rand_", n_x, ".png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
pole_sort <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort)) + # , col = brd_col, size = brd_size) +
labs(x = "4", y = expression(psi)) +
coord_polar() +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_sort
# Save current plot:
plot_name <- paste0("pix/pole_sort_", n_x, ".png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)- With thin borders and a numeric label (of the final slice):
# Parameters:
cur_lbl <- paste0(n_x)
if (n_y == 1) {
y_lbl <- n_y + 0 # no correction
} else if (n_y == 2) {
y_lbl <- n_y + .75 # small correction
} else if (n_y < 4) {
y_lbl <- n_y + 1 # constant correction
} else {
y_lbl <- n_y + n_y/4 # scaled correction (increasing with n_y)
}
# (a) rand version with label:
pole_rand_lbl <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = rand), col = brd_col, size = brd_size) +
# labs(title = "ds4psy") +
# scale_x_continuous(breaks = c(0:n_x), labels = c(0:n_x)) +
# geom_vline(xintercept = n_x, size = 1, color = "gold") +
geom_text(x = n_x, y = y_lbl, label = cur_lbl, size = 2) +
coord_polar() +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_rand_lbl
# Save current plot:
plot_name <- paste0("pix/pole_rand_", n_x, "_brd_lbl.png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)
# (a) sort version with label:
pole_sort_lbl <- ggplot(tb) +
geom_tile(aes(x = x, y = y, fill = sort), col = brd_col, size = brd_size) +
# labs(title = "ds4psy") +
# scale_x_continuous(breaks = c(0:n_x), labels = c(0:n_x)) +
# geom_vline(xintercept = n_x, size = 1, color = "gold") +
geom_text(x = n_x, y = y_lbl, label = cur_lbl, size = 2) +
coord_polar() +
labs(x = "Data", y = "Science") +
# scale_fill_continuous(low = "white", high = seeblau) +
scale_fill_gradientn(colors = unikn_sort) +
ds_theme
pole_sort_lbl
# Save current plot:
plot_name <- paste0("pix/pole_sort_", n_x, "_brd_lbl.png")
ggsave(plot_name, width = plot_size, height = plot_size, units = c("cm"), dpi = 300)Combine plots
plot_grid(tile_rand, tile_sort)
plot_grid(pole_rand, pole_sort)
plot_grid(tile_rand, pole_sort)Waves
Data:
N <- 100000
df <- tibble(x = 1:N,
a = rnorm(n = N, mean = 0, sd = 250),
# b = rnorm(n = N, mean = 100, sd = 200),
c = rnorm(n = N, mean = 500, sd = 200)
)
# dfDensity plots:
lwd_all <- 3
ggplot(df) +
geom_density(aes(x = a), col = seeblau, fill = "black", lwd = lwd_all, alpha = .25) +
# geom_density(aes(x = b), col = "black", fill = unikn.pal[[5]], alpha = .33) +
geom_density(aes(x = c), col = "black", fill = seeblau, lwd = lwd_all, alpha = .33) +
theme_void()Histograms (with function curves):
lwd_all <- .75
# overlay histogram and normal density
ggplot(df) +
geom_histogram(aes(x = c, y = stat(density)), binwidth = 150, col = "white", fill = "black", alpha = .25) +
geom_histogram(aes(x = a, y = stat(density)), binwidth = 150, col = "white", fill = seeblau, alpha = .25) +
stat_function(fun = dnorm, args = list(mean = mean(df$c), sd = sd(df$c)), lwd = lwd_all, col = "black", alpha = .95) +
stat_function(fun = dnorm, args = list(mean = mean(df$a), sd = sd(df$a)), lwd = lwd_all, col = seeblau, alpha = .95) +
scale_x_continuous(limits = c(-2000, 1400)) +
theme_void() # + # annotate(geom = "text", x = -1900, y = .0018, label = course_title, color = "black", alpha = .90, adj = 0, fontface = 2, size = 2.5)Waves (as curves):
# as curves:
lwd_all <- .75
ggplot(df) +
stat_function(fun = dnorm, args = list(mean = 300, sd = 275), lwd = lwd_all, col = "black", alpha = .99) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 300), lwd = lwd_all, col = seeblau, alpha = .40) +
stat_function(fun = dnorm, args = list(mean = 600, sd = 250), lwd = lwd_all, col = "black", alpha = .40) +
stat_function(fun = dnorm, args = list(mean = 900, sd = 225), lwd = lwd_all, col = seeblau, alpha = .75) +
scale_x_continuous(limits = c(-4000, 1700)) +
theme_void() # + # annotate(geom = "text", x = -3900, y = .0016, label = course_title, color = "black", alpha = .90, adj = 0, fontface = 2, size = 2.5)
ggsave("pix/waves.png", width = 15, height = 1.5, units = c("cm"), dpi = 300)Ideas
ToC bar chart
Idea: Format ToC as bar chart with polar coordinates.
+++ here now +++
# N <- 10 # number of chapters/topics
# nr <- 1:N
# tp <- c("Introduction", "Chapter 2", "Chapter 3", "Chapter 4", "Chapter 5",
# "Chapter 6", "Chapter 7", "Chapter 8", "Chapter 9", "Chapter 10")
# val <- 10 + nr
# Table:
toc <- tribble(
~nr, ~tp, ~ctr,
0, "Introduction", 5,
1, "Basic R", 10, # was: "Basic R concepts and commands",
2, "Visualizing data", 8,
3, "Transforming data", 9,
4, "Exploring data (EDA)", 10,
5, "Tibbles", 6,
6, "Importing data", 5,
7, "Tidying data", 9,
8, "Joining data", 7,
9, "Functions", 8,
10, "Iteration", 7
)
toc <- toc %>% mutate(nr_val = nr + 10) # add constant to increase overall height of bars
# Parameters:
N <- nrow(toc)
max_nr_val <- max(toc$nr_val)
min_ctr <- min(toc$ctr)
max_ctr <- max(toc$ctr)
tol <- 4 # tolerance value (for text labels)
txt_size <- 2.5 # size of txt labels (below)
## Colors: ------
# unikn.pal # basic
unikn_toc <- c(rev(unikn.pal[5:10]), # white:black (6)
unikn.pal[1:4], # seeblau1 to 4 (4)
seeblau) # seeblau4 (1)
# unikn_toc # 11 colors
## Plots: ------
# Chronology:
bar_hori <- ggplot(toc, aes(x = nr)) +
geom_bar(aes(y = ctr, fill = nr_val), stat = "identity", color = grey(.5, 1)) +
scale_fill_gradientn(colors = unikn_toc) +
geom_text(aes(y = ctr + .25, label = tp), angle = 0, adj = 0, size = txt_size) +
scale_x_reverse(breaks = 0:N, labels = 0:N) +
# scale_x_continuous(breaks = 0:N, labels = 0:N) +
scale_y_continuous(limits = c(0, (max_ctr + tol)), breaks = 1:max_ctr, labels = 1:max_ctr) +
theme_minimal() +
theme(legend.position = "none", panel.grid.minor = element_blank(), panel.grid.major.x = element_blank()) +
coord_flip() +
labs(title = "ds4psy Chronology", x = "Session", y = "Relevance")
bar_hori
# Relevance:
# (1) Bar plots:
bar_vert <- ggplot(toc, aes(x = nr)) +
geom_bar(aes(y = ctr, fill = ctr), stat = "identity", color = grey(.5, 1)) +
# scale_fill_gradientn(colors = unikn_toc) +
scale_fill_gradient(low = "white", high = seeblau, limits = c(min_ctr, max_ctr)) +
geom_text(aes(y = ctr + .5, label = tp), angle = 90, adj = 0, size = txt_size) +
scale_x_continuous(breaks = 0:N, labels = 0:N) +
scale_y_continuous(limits = c(0, (max_ctr + tol)), breaks = 1:max_ctr, labels = 1:max_ctr) +
theme_minimal() +
theme(legend.position = "none", panel.grid.minor = element_blank()) +
labs(title = "ds4psy Relevance", x = "Session", y = "Relevance")
bar_vert
# (2) Plots on polar coordinates:
topic_clock <- ggplot(toc, aes(x = nr)) +
geom_bar(aes(y = ctr, fill = nr_val), stat = "identity", color = grey(0, 1), size = .25) +
scale_fill_gradientn(colors = unikn_toc) +
# scale_fill_gradient(low = "white", high = seeblau, limits = c(min_ctr, max_ctr)) +
geom_text(aes(y = max_ctr + tol/2, label = tp), adj = .5, size = txt_size) +
scale_x_continuous(breaks = 0:N, labels = 0:N) +
scale_y_continuous(limits = c(0, (max_ctr + tol)), breaks = 1:max_ctr, labels = 1:max_ctr) +
coord_polar() +
# labs(title = "Centrality of topic", x = "Chapter", y = "Topic") +
# theme_void() +
theme_light() +
theme(legend.position = "none", axis.line = element_blank(),
axis.title = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), panel.border = element_blank())
# topic_clock
centrality <- ggplot(toc, aes(x = nr)) +
geom_bar(aes(y = ctr, fill = ctr), stat = "identity", color = grey(.10, 1), size = .25) +
# scale_fill_gradientn(colors = unikn_toc) +
scale_fill_gradient(low = "white", high = seeblau, limits = c(min_ctr, max_ctr)) +
geom_text(aes(y = (max_ctr + .65 * tol), label = tp), adj = .5, size = txt_size) +
scale_x_continuous(breaks = 0:N, labels = 0:N) +
scale_y_continuous(limits = c(0, (max_ctr + tol)), breaks = 1:max_ctr, labels = 1:max_ctr) +
coord_polar() +
# theme_void() +
theme_light() +
theme(#legend.position = "none",
axis.line = element_blank(),
axis.title = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), panel.border = element_blank()) +
labs(fill = "Centrality:")
centrality
# Highlight current session:
nr_session <- 2
# Add to toc:
toc$session_nr <- 0
toc$session_nr[(nr_session + 1)] <- (max_ctr + tol) # non-zero value (max)
toc
#> # A tibble: 11 x 5
#> nr tp ctr nr_val session_nr
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 0 Introduction 5 10 0
#> 2 1 Basic R 10 11 0
#> 3 2 Visualizing data 8 12 14
#> 4 3 Transforming data 9 13 0
#> 5 4 Exploring data (EDA) 10 14 0
#> 6 5 Tibbles 6 15 0
#> 7 6 Importing data 5 16 0
#> 8 7 Tidying data 9 17 0
#> 9 8 Joining data 7 18 0
#> 10 9 Functions 8 19 0
#> 11 10 Iteration 7 20 0
cur_session_clock <- ggplot(toc, aes(x = nr)) +
geom_bar(aes(y = session_nr), stat = "identity", fill = "gold", alpha = .75) +
geom_bar(aes(y = ctr, fill = ctr), stat = "identity", color = grey(.10, 1), size = .25) +
# scale_fill_gradientn(colors = unikn_toc) +
scale_fill_gradient(low = "white", high = seeblau, limits = c(min_ctr, max_ctr)) +
geom_text(aes(y = (max_ctr + .65 * tol), label = tp), adj = .5, size = txt_size) +
scale_x_continuous(breaks = 0:N, labels = 0:N) +
scale_y_continuous(limits = c(0, (max_ctr + tol)), breaks = 1:max_ctr, labels = 1:max_ctr) +
coord_polar() +
# theme_void() +
theme_light() +
theme(#legend.position = "none",
axis.line = element_blank(),
axis.title = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), panel.border = element_blank()) +
labs(fill = "Centrality:")
cur_session_clock- Clock with polar coordinates
[This file last updated on 2019-02-10 16:03:44 by hn.]